﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;

namespace MyXmlTools
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 读取XML的所有子节点
        /// </summary>
        /// <param name="filePath">XML文件绝对路径</param>
        /// <param name="xPath">范例: @"Skill/First/SkillItem"</param>
        /// <returns></returns>
        public static XmlNodeList ReadNodes(string filePath, string xPath)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(filePath);

                XmlNode xn = doc.SelectSingleNode(xPath);
                XmlNodeList xnList = xn.ChildNodes;  //得到该节点的子节点
                return xnList;
            }
            catch(Exception ex)
            {
                return null;
            }
        }


        public static void CreateNode(XmlDocument xmlDoc, XmlNode parentNode, string name, string value)
        {
            XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, null);
            if (!string.IsNullOrEmpty(value))
            {
                node.InnerText = value;
            }
            parentNode.AppendChild(node);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string path;
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                path = folderBrowserDialog.SelectedPath + "\\";
            }
            else
            {

                MessageBox.Show("请选择一个xml目录");
                return;
            }
            string[] files2 = Directory.GetFiles(path, "*.xml");

            DataTable dt = new DataTable();
            dt.Columns.Add("orderid");
            dt.Columns.Add("orderdate");
            dt.Columns.Add("orderprice");
            dt.Columns.Add("orderfile");

            foreach (string file in files2)
            {
                //将XML文件加载进来
                XDocument document = XDocument.Load(file);
                //获取到XML的根元素进行操作
                XElement root = document.Root;
                IEnumerable<XElement> enumerable = root.Elements();
                foreach (XElement item in enumerable)
                {
                    XElement AffiliateOrderId = item.Element("AffiliateOrderId");
                    XElement Date = item.Element("Date");
                    XElement price = item.Element("Value");
                    DataRow dr = dt.NewRow();
                    dr["orderid"] = AffiliateOrderId.Value;
                    dr["orderdate"] = Date.Value;
                    dr["orderprice"] = price.Value;
                    dr["orderfile"] = file;


                    dt.Rows.Add(dr);


                }

                //XmlNodeList personNodes = ReadNodes(file, "/Orders");
                //foreach (XmlNode xnode in personNodes)
                //{
                //    DataRow dr = dt.NewRow();
                //    dr["orderid"] = xnode.SelectSingleNode("//AffiliateOrderId").InnerText;
                //    dr["orderdate"] = xnode.SelectSingleNode("//Date").InnerText;
                //    dr["orderfile"] = file;
                //    dt.Rows.Add(dr);
                //}
            }

            string excelpath = path + "所有上传过的剑桥订单" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls";
            AsposeExcel ae = new AsposeExcel(excelpath);
            ae.DatatableToExcel(dt, "aa");


        }

        private void button2_Click(object sender, EventArgs e)
        {

  

            XmlDocument xmldoc = new XmlDocument();
            XmlNode node = xmldoc.CreateXmlDeclaration("1.0", "UTF-8", null);
            xmldoc.AppendChild(node);

            XmlElement root = xmldoc.CreateElement("itemRule");

            XmlElement title = createXmlNode(xmldoc, "field", "title", "标题");
            CreateNode(xmldoc, title, "value", "hello");
            root.AppendChild(title);

            XmlElement subtitle = createXmlNode(xmldoc, "field", "subtitle", "子标题");
            CreateNode(xmldoc, subtitle, "value", "zihello");
            root.AppendChild(subtitle);

            XmlElement category = createXmlNode(xmldoc, "field", "category", "类目");
            CreateNode(xmldoc, category, "value", "{\"cat_id\":\"" + "124741" + "\"}");
            root.AppendChild(category);

            xmldoc.AppendChild(root);

        }

        private XmlElement createXmlNode(XmlDocument xmldoc,string EleName,string id,string name)
        {
            XmlElement xmlElement = xmldoc.CreateElement(EleName);

            XmlAttribute attrID = xmldoc.CreateAttribute("id");
            XmlAttribute attrName = xmldoc.CreateAttribute("name");
            XmlAttribute attrType = xmldoc.CreateAttribute("type");
            attrID.Value = id;
            attrName.Value = name;
            attrType.Value = "input";
            xmlElement.Attributes.Append(attrID);
            xmlElement.Attributes.Append(attrName);
            xmlElement.Attributes.Append(attrType);
            return xmlElement;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            CreateWeb("https://reebonz.tmall.hk/p/rd279654.htm");
        }
        public string CreateWeb(string url)
        {

            StringBuilder sb = new StringBuilder();
            //抓取网页
            System.Net.WebRequest request = System.Net.WebRequest.Create(url);
            System.Net.WebResponse response = request.GetResponse();
            //读取文件流
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")); //reader.ReadToEnd() 表示取得网页的源码
            //FileStream fs = new FileStream("~/baidu.html", FileMode.OpenOrCreate);
            string strhtml = reader.ReadToEnd();
            //正则匹配网站的图片标签
            string Rxg = @"recommendItemIds=([\s\S]*?)shopId=";
            //匹配出图片标签的集合
            MatchCollection mc = Regex.Matches(strhtml, Rxg);
            for (int i = 0; i < mc.Count; i++)
            {
                sb.Append(mc[i]);

            }
            //返回图片标签HTML输出
            return sb.ToString();
        }
    }
}
